package presentation;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import control.CControl;

public class PMain extends JPanel {
	private static final long serialVersionUID = 1L;
	private CControl cControl;
	private JFrame[] windows;
	private String[] tabs = {"Live view", "Car", "Monitor", "Network status",
			"EPL nodes","Settings"};

	public PMain() {
		cControl = new CControl();
		
		this.setBackground(Color.white);
		this.setLayout(new BorderLayout());
	}
	
	public void createUserInterface() {
		String panelSetting = cControl.getSettingsParameter("panelsettings");
		
		if (panelSetting != null && panelSetting.equalsIgnoreCase("Separate")) {
			windows = new JFrame[6];
			Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
			Image image = null;
	        try {
	        	String rootPath = new File(CControl.class.
	    				getProtectionDomain().getCodeSource().getLocation().
	    				getPath()).getParent() + "/";
	        	image = ImageIO.read(new File(rootPath + "img/icon.png"));
	        } catch (IOException e) {}
	        
			for (int i = 0; i < windows.length; i++) {
				windows[i] = new JFrame("SDU vikings Liveview: " + tabs[i]);
				windows[i].setIconImage(image);
				windows[i].setResizable(true);
				windows[i].setSize(1024,768);
				windows[i].setMinimumSize(new Dimension(1024, 768));
				windows[i].setLocation(dim.width/2-512+i*20,dim.height/2-384+i*20);
				if (i == 0) {
					windows[i].setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				} else {
					windows[i].setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
				}
			}
			
			windows[0].getContentPane().add(new PLiveview(cControl));
			windows[1].getContentPane().add(new PCar(cControl));
			windows[2].getContentPane().add(new PMonitor(cControl));
			windows[3].getContentPane().add(new PNetworkStatus(cControl));
			windows[4].getContentPane().add(new PEPLNodes(cControl));
			windows[5].getContentPane().add(new PSettings(cControl));
		} else {
			windows = new JFrame[2];
			windows[0] = new JFrame("Details");
			windows[1] = new JFrame("SDU vikings Liveview");
			Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

			Image image = null;
	        try {
	        	String rootPath = new File(CControl.class.
	    				getProtectionDomain().getCodeSource().getLocation().
	    				getPath()).getParent() + "/";
	        	image = ImageIO.read(new File(rootPath + "img/icon.png"));
	        } catch (IOException e) {}
	        windows[0].setIconImage(image);
			
	        windows[0].setResizable(true);
	        //windows[0].setSize(1024,768);
	        windows[0].setMinimumSize(new Dimension(1024, 768));
	        windows[0].setExtendedState(JFrame.MAXIMIZED_BOTH);
	        windows[0].setLocation(0,dim.height/2-384);
	        windows[0].setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	        
	        windows[1].setIconImage(image);
	        windows[1].setExtendedState(JFrame.MAXIMIZED_BOTH); 
	        windows[1].setResizable(true);
	        //windows[1].setSize(1024,768);
	        
	        windows[1].setMinimumSize(new Dimension(1024, 768));
	        windows[1].setLocation(dim.width-1024,dim.height/2-384);
	        windows[1].setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			JTabbedPane tabs = new JTabbedPane();
			//tabs.addTab("Live view", new PLiveview(cControl));
			windows[1].getContentPane().add(new PLiveview(cControl));
			tabs.addTab("Battery Pack", new PBatteryOverview(cControl));
			tabs.addTab("Motor", new PMotorOverview(cControl));
			tabs.addTab("Car", new PCar(cControl));
			tabs.addTab("Monitor", new PMonitor(cControl));
			tabs.addTab("Network status", new PNetworkStatus(cControl));
			tabs.addTab("EPL nodes", new PEPLNodes(cControl));
			tabs.addTab("Error log", new PNodeErrorPanel(cControl,"_ErrorReg"));
			tabs.addTab("Status log", new PNodeErrorPanel(cControl,"_StatusReg"));
			tabs.addTab("Settings", new PSettings(cControl));
			tabs.setBackground(Color.white);
			tabs.setSelectedIndex(0);
			
			this.add(tabs,BorderLayout.CENTER);
			this.add(new PStatusBar(cControl),BorderLayout.PAGE_END);
			
			windows[0].getContentPane().add(this);
		}
		
	}
	
	public void showGUI() {
		for (int i = windows.length-1; i >= 0; i--) {
			windows[i].setVisible(true);
			windows[i].toFront();
		}
	}
	
	public void startStreaming() {
		cControl.startStreaming();
	}
}


